'''
爬虫系统主页入口
'''
from datetime import datetime
import time
from modules import pcUtils
from modules import dbUtils
from flask import Flask,render_template

app = Flask(__name__)
pc = pcUtils.pcModel()

@app.route("/getData")
def getData():
    db = dbUtils.dbModel()
    #1.选取爬的目标，争取爬下来
    #1.1 确定网站地址
    root_url = "www.demo.com"  
    #1.2 爬取目标，返回数据列表
    targetlist = pc.getTargetText(url=root_url,selector="")
    #    假设爬取的如下newslist
    newslist = ["news1_title","news2_title","news3_title"]
    #2.直接把爬去的内容灌进数据库
    for title in newslist:
        ctime = datetime.now()
        db.insertData(title=title,source="",ctime=ctime)
        time.sleep(1)

    #3.查看是否入库
    print(db.queryData())

@app.route("/showData")
def showData():
    db = dbUtils.dbModel()
    #1.1 查数据库里的爬取内容
    res = db.queryData()
    #    例如存储结果查询如下
    res = [(1,"newstitle1","source1","2023-6-11"),(2,"newstitle2","source2","2023-6-11"),(3,"newstitle3","source3","2023-6-12")]
    #1.2 将存储的内容传递到网页前端
    return render_template("index.html",data=res)

if __name__=='__main__':
    app.run()
